home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / UIFlow 1.0.1 / UIFlow Source / VSet2.0 / temp / vg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-16  |  15.0 KB  |  679 lines  |  [TEXT/????]

  1. /*****************************************************************************
  2. *              NCSA HDF Vset release 2.1
  3. *                    May 1991
  4. *
  5. * NCSA HDF Vset release 2.1 source code and documentation are in the public
  6. * domain.  Specifically, we give to the public domain all rights for future
  7. * licensing of the source code, all resale rights, and all publishing rights.
  8. * We ask, but do not require, that the following message be included in all
  9. * derived works:
  10. * Portions developed at the National Center for Supercomputing Applications at
  11. * the University of Illinois at Urbana-Champaign.
  12. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  13. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  14. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  15. *****************************************************************************
  16. * Likkai Ng NCSA May 1991
  17. *
  18. * vg.c
  19. * Part of the HDF VSet interface
  20. * This file contains routine to handle VDATAs.
  21. *
  22. * Most routines return -1 (FAIL) on error. 
  23. * VSattach returns NULL on error.
  24. *
  25. * PRIVATE functions manipulate vsdir and are used only within this file.
  26. * PRIVATE data structures in here pertain to vdata in vsdir only.
  27. *
  28. **************************************************************************/
  29.  
  30. #include <ctype.h> 
  31. #include "vg.h"
  32.  
  33. PRIVATE int newref_count = -1; /* used by Vnewref only */
  34.  
  35. /* ------------------------------------------------------------------
  36. *    Vnewref
  37. *    utility routine. returns a unique reference number.
  38. *
  39. *    NOTE: should be modified to call DFnewref!!!
  40. *  currently looks once in vsdir and vgdir, and find the highest ref.
  41. *    also, the parameter f isn't used right now.
  42. *
  43. *    RETURNS a unique ref (+ve integer) ,
  44. *    RETURNS -1 if error
  45. *
  46. *  undocumented
  47. *
  48. */
  49.  
  50. int vnewref (f)            /*@@*/
  51.  
  52. DF  *f;
  53. {
  54. #if 0
  55.     int t;
  56.  
  57.  
  58.     if (f==NULL) return(FAIL);
  59.     if (newref_count == -1) {
  60.         for(t=1;t<65300;t++) {
  61.             if      ( -1 != vexistvg(f,t))   continue;
  62.             else if ( -1 != vexistvs(f,t))   continue;
  63.             else break;
  64.         }
  65.         newref_count = t;
  66.     }
  67.  
  68.     newref_count++;
  69.     if (newref_count > 65300) return(FAIL);
  70.  
  71.     return( newref_count - 1 );
  72. #endif
  73.  
  74. #if 1
  75.     return( DFnewref(f) );
  76. #endif
  77.  
  78. } /* vnewref */
  79.  
  80.  
  81. /* ------------------------------------------------------------------
  82. * VSelts
  83. * returns the number of elements in the VDATA vs 
  84. * returns -1  on error.
  85. *
  86. * undocumented
  87. * 28-MAR-91 Jason NG NCSA
  88. *
  89. */
  90.  
  91. PUBLIC int32 VSelts (vs)                       /*@-@*/
  92.  
  93. VDATA *vs;
  94. {
  95.     return( (vs->otag==VSDESCTAG) ?  vs->nvertices : -1);
  96.  
  97. } /* VSelts */
  98.  
  99.  
  100.  
  101. /* ------------------------------------------------------------------
  102. *    VSgetinterlace 
  103. *  returns the interlace (in the file) of the vdata vs.
  104. *  returns -1 on error.
  105. *
  106. *  undocumented
  107. *
  108. */
  109.  
  110. PUBLIC int VSgetinterlace (vs)              /*@-@*/
  111.  
  112. VDATA *vs;
  113. {
  114.     return( (vs==NULL) ? FAIL : vs->interlace );
  115.  
  116. } /* VSgetinterlace */
  117.  
  118.  
  119.  
  120. /* ------------------------------------------------------------------
  121. *    VSsetinterlace 
  122. *     sets the vdata's interlace to full or none.
  123. *    returns -1 on error.
  124. */
  125.  
  126. PUBLIC int VSsetinterlace (vs, interlace)      /*@-@*/
  127.  
  128. VDATA *vs;
  129. int interlace;
  130. {
  131.     if (vs==NULL)
  132.         return(FAIL);
  133.     if ( vs->access == 'r' ||            /* only for write-only files */
  134.     vs->nvertices > 0)             /*may not change interlace if data exists*/
  135.         return(FAIL);
  136.  
  137.     /* currently only 2 kinds allowed */
  138.  
  139.     if ( interlace==FULL_INTERLACE || 
  140.         interlace==NO_INTERLACE ) {
  141.         vs->interlace = interlace;
  142.         return(1); /* ok */
  143.     }
  144.     else 
  145.         return(FAIL);                            /* bad interlace type */
  146.  
  147. } /* VSsetinterlace */
  148.  
  149.  
  150. /* ------------------------------------------------------------------
  151. *    VSgetfields 
  152. *  returns the fieldnames in a vdata.
  153. *  RETURNS  -1 on error, else the no of fields in the vdata.
  154. *
  155. *    undocumented
  156. *
  157. */
  158.  
  159. PUBLIC int VSgetfields (vs,fields)        /*@-@*/
  160.  
  161. VDATA *vs;
  162. char  *fields;                            /* fieldnames are returned in this var */
  163. {
  164.     int i;
  165.     if (vs==NULL) return(FAIL);
  166.  
  167.     fields[0] = '\0';
  168.     for (i=0;i<vs->wlist.n;i++) { /* build the comma-separated string */
  169.         strcat(fields,vs->wlist.name[i]);
  170.         if ( i < vs->wlist.n - 1 )
  171.             strcat(fields,",");
  172.     }
  173.  
  174.     return(vs->wlist.n);
  175.  
  176. } /* VSgetfields */
  177.  
  178. /* ------------------------------------------------------------------
  179. *    VSfexist
  180. *     tests for existence of 1 or more fields in a vdata.
  181. *
  182. *    RETURNS -1 if flase, or error
  183. *    RETURNS 1 if true
  184. */
  185.  
  186. PUBLIC int VSfexist (vs, fields)          /*@-@*/
  187.  
  188. VDATA *vs;
  189. char    *fields;
  190. {
  191.     char           **av, *s;
  192.     int            ac,i,j,found;
  193.     VWRITELIST    *w;
  194.  
  195.     if (vs==NULL) return(FAIL);
  196.  
  197.     /* call scanattrs to parse the string */
  198.  
  199.     if (scanattrs(fields,&ac,&av) < 0) return(FAIL); /* bad attrs string */
  200.     if (ac<1) return(FAIL);                                 /* no attrs */
  201.  
  202.     /* now check in vs's field table */
  203.  
  204.     w = &vs->wlist;
  205.     for (i=0;i<ac;i++) {
  206.         for (found=0,s=av[i],j=0;j<w->n;j++) {
  207.             if ( matchnocase(s,w->name[j]) ) {
  208.                 found=1;
  209.                 break;
  210.             }
  211.         }
  212.         if (!found) return(FAIL);
  213.     }
  214.  
  215.     return(1);
  216.  
  217. } /* VSfexist */
  218.  
  219.  
  220. /* ================================================================== */
  221. /*
  222. *    VSsizeof - computes the byte size of the field(s) of a vdata.
  223. *             - Note that the size is the actual size for the local machine.
  224. *
  225. *                - RETURNS -1 on error, else the field(s) size (+ve integer).
  226. */
  227.  
  228. PUBLIC int VSsizeof (vs, fields)           /*@-@*/
  229.  
  230. VDATA *vs;
  231. char  *fields;
  232. {
  233.     int     totalsize, ac, i,j,found;
  234.     char   **av;
  235.  
  236.     if (vs==NULL)                              return(FAIL);
  237.  
  238.     if (scanattrs(fields,&ac,&av) < 0) return(FAIL);     /* bad attrs string */
  239.     if (ac<1)                                  return(FAIL);     /* no attrs */
  240.  
  241.     if (vjv) { 
  242.         sprintf(sjs,"#VSsizeof: fields are [%s]\n",fields);
  243.         zj; 
  244.     }
  245.  
  246.     totalsize=0;
  247.     for (i=0;i<ac;i++) {
  248.         for (found=0,j=0;j<vs->wlist.n;j++)
  249.             if (!strcmp(av[i], vs->wlist.name[j])) {  /* check fields in vs */
  250.                 totalsize += vs->wlist.esize[j];
  251.                 found=1;
  252.                 break;
  253.             }
  254.  
  255.         if (!found) {
  256.             sprintf(sjs,"@VSsizeof:[%s] not in vs\n",av[i]);
  257.             zj;
  258.             return(FAIL);
  259.         }
  260.     }
  261.  
  262.     return(totalsize);
  263.  
  264. } /* VSsizeof */
  265.  
  266. /* ================================================================== */
  267.  
  268. /* matchnocase -  (PRIVATE) compares 2 strings, ignoring case 
  269. *                   returns 1 if match, else 0
  270. */
  271.  
  272. PRIVATE int matchnocase (strx,stry)     /*@@*/
  273.  
  274. char *strx,*stry;
  275. {
  276.     int     i,nx,ny,tx,ty;
  277.     char     *sx, *sy;
  278.  
  279.     nx = strlen(strx);
  280.     ny = strlen(stry);
  281.     if ( nx != ny) return(0);  /* different lengths */
  282.  
  283.     for (sx=strx, sy=stry, i=0;i<nx;i++,sx++,sy++) {
  284.         tx= *sx; 
  285.         ty= *sy;
  286.         if (islower(tx)) tx=toupper(tx);
  287.         if (islower(ty)) ty=toupper(ty);
  288.         if (tx != ty)       return(0);
  289.     }
  290.  
  291.     return(1);
  292.  
  293. } /* matchnocase */
  294.  
  295.  
  296. /* ================================================================== */
  297.  
  298. /*
  299. *    VSdump - prints contents of a vdata (for debugging) 
  300. *                no return codes.
  301. */
  302.  
  303. void VSdump (vs)                           /*@@*/
  304.  
  305. VDATA *vs;
  306. {
  307.     VWRITELIST    *w;
  308.     int             i;
  309.  
  310.     if (vs==NULL) {
  311.         sprintf(sjs,"@VSdump: vs is null\n");
  312.         zj;
  313.         return;
  314.     }
  315.  
  316.     sprintf(sjs,"@tag=%d ref=%d i=%d ",vs->otag, vs->oref,vs->interlace);
  317.     zj;
  318.     sprintf(sjs,"@nv=%d\n ",vs->nvertices);
  319.     zj;
  320.  
  321.     w = (VWRITELIST*) &vs->wlist;
  322.     sprintf(sjs,"@vsize(HDF)=%d fields=%d [%s]\n",w->ivsize,w->n,vs->vsname);
  323.     zj;
  324.  
  325.     for (i=0;i<w->n;i++)
  326.     { 
  327.         sprintf(sjs,"@<%s>      type:%d esize=%d isize=%d off=%d\n",
  328.             w->name[i], w->type[i], w->esize[i],w->isize[i],w->off[i]);
  329.         zj;
  330.     }
  331.  
  332.     sprintf(sjs,"\n");
  333.     zj;
  334.  
  335. } /* VSdump */
  336.  
  337.  
  338. /* ======================================================= */
  339. /*
  340. *    VSsetname - give a name to a vdata.
  341. *              - NO RETURN VALUES
  342. *              - truncates to max length of VSNAMELENMAX
  343. *      
  344. */
  345.  
  346. PUBLIC void VSsetname (vs, vsname)           /*@-@*/
  347.  
  348. VDATA *vs;
  349. char    *vsname;
  350.  
  351. {
  352.     if (vs == NULL) return;
  353.     if ( strlen(vsname) > VSNAMELENMAX) {
  354.         strncpy(vs->vsname, vsname,VSNAMELENMAX);
  355.         vs->vsname[VSNAMELENMAX]='\0';
  356.     }
  357.     else 
  358.         strcpy(vs->vsname, vsname);
  359.     vs->marked = 1;
  360.     return;
  361.  
  362. } /* VSsetname */
  363.  
  364. /* ======================================================= */
  365. /*
  366. *    VSsetclass- assigns a class name to a vdata.
  367. *              - NO RETURN VALUES
  368. *              - truncates to max length of VSNAMELENMAX
  369. *      
  370. */
  371.  
  372. PUBLIC void VSsetclass (vs, vsclass)           /*@-@*/
  373.  
  374. VDATA *vs;
  375. char    *vsclass;
  376.  
  377. {
  378.     if (vs == NULL) return;
  379.     if ( strlen(vsclass) > VSNAMELENMAX) {
  380.         strncpy(vs->vsclass, vsclass,VSNAMELENMAX);
  381.         vs->vsclass[VSNAMELENMAX]='\0';
  382.     }
  383.     else 
  384.         strcpy(vs->vsclass, vsclass);
  385.     vs->marked = 1;
  386.     return;
  387.  
  388. } /* VSsetclass*/
  389.  
  390. /* ======================================================= */
  391.  
  392. /*
  393. *    VSgetname - gets the vdata's name.
  394. *                 - NO RETURN VALUES
  395. */
  396.  
  397. PUBLIC void VSgetname (vs, vsname)           /*@-@*/
  398.  
  399. VDATA *vs;
  400. char    *vsname;
  401. {
  402.     if (vs != NULL) strcpy(vsname, vs->vsname);
  403.     return;
  404.  
  405. } /* VSgetname */
  406.  
  407. /* ======================================================= */
  408.  
  409. /*
  410. *    VSgetclass - gets the vdata's class name.
  411. *                 - NO RETURN VALUES
  412. */
  413.  
  414. PUBLIC void VSgetclass (vs, vsclass)           /*@-@*/
  415.  
  416. VDATA *vs;
  417. char    *vsclass;
  418. {
  419.     if (vs != NULL) strcpy(vsclass, vs->vsclass);
  420.     return;
  421.  
  422. } /* VSgetclass */
  423.  
  424.  
  425. /* ================================================================== */
  426. /*
  427. *    VSinquire - gets info about a vdata vs:
  428. *
  429. *                  nvertices:     no of vertices in it.
  430. *                  interlace:     its interlcae
  431. *                  fields :         a comma separated string listing the field(s). 
  432. *                                    (eg "PX,PY")
  433. *                  eltsize:        size of element (all field(s)) on local machine.
  434. *                  vsname:         vdata's name, if any.
  435. *
  436. *    RETURNS -1 if error
  437. *    RETURNS 1 if ok
  438. *
  439. */
  440.  
  441.  
  442. PUBLIC int VSinquire (vs, nelt, interlace, fields, eltsize, vsname)   /*@-@*/
  443.  
  444. VDATA     *vs;
  445. char      *fields, *vsname;
  446. int         *nelt, *interlace, *eltsize;
  447.  
  448. {
  449.     if (vs==NULL) return(FAIL);
  450.     VSgetfields (vs,fields);
  451.  
  452.     *nelt       = vs->nvertices;
  453.     *interlace  = vs->interlace;
  454.     *eltsize    =  VSsizeof (vs,fields);
  455.     strcpy(vsname,vs->vsname);
  456.  
  457.     return(1); /* ok */
  458.  
  459.  
  460. } /* VSinquire */
  461.  
  462. /* ================================================================== */
  463. /*
  464. * VSlone - returns an array of refs of all lone vdatas in the file.
  465. *           - returns -1 if error
  466. *          - otherwise returns the total number of lone vdatas in the file 
  467. *
  468. *            If idarray is too small, routine will only fill idarray with up
  469. *             to asize worth of refs.
  470. *
  471. *            INPUT idarray: user supplies  an int array.
  472. *           INPUT asize: integer specifying how many ints in idarray[];
  473. *            INPUT f: HDF file pointer.
  474. *
  475. */
  476.  
  477. PUBLIC int VSlone(f, idarray, asize) 
  478.  
  479. DF * f;
  480. int idarray[];             /* array to contain the refs */
  481. int asize;            /* input: size of idarray */
  482. {
  483. int      * lonevdata; /* local working area: stores flags of vdatas */
  484. int         i,vgid, vsid, vstag;
  485. VGROUP     * vg;
  486. int         dum;
  487. int         nlone; /* total number of lone vdatas */
  488.  
  489.  
  490. /* -- allocate space for vdata refs, init to zeroes -- */
  491. if (NULL == (lonevdata = (int*) DFIgetspace( 65000 * sizeof(int)))) 
  492.    RTNEG ("VSlone: space too low. VSlone not executed\n");
  493. for(i=0;i<65000;i++) lonevdata[i] = 0;
  494.  
  495. /* -- look for all vdatas in the file, and flag (1) each -- */
  496. vsid = -1;
  497. dum=0;
  498. while(1) {
  499.     if ( -1 == (vsid = VSgetid (f, vsid))) break; /* no more vdatas */
  500.     lonevdata[vsid ] = 1;
  501.     }
  502.  
  503. /* -- Look through all vgs, searching for vdatas -- */
  504. /* -- increment its index in lonevdata if found -- */
  505. dum=0;
  506. vgid = -1;
  507. while(1) {
  508.     if ( -1 == (vgid = Vgetid (f, vgid))) break; /* no more vgroups */
  509.     vg = (VGROUP*) Vattach(f,vgid,"r");
  510.     for (i=0; i< Vntagrefs(vg); i++) {
  511.         Vgettagref (vg, i, &vstag, &vsid);
  512.         if (vstag==VSDESCTAG) 
  513.             { lonevdata[vsid]++; dum++;  }
  514.         }
  515.     Vdetach(vg);
  516.     }
  517.  
  518. /* -- check in lonevdata: it's a lone vdata if its flag is still 1 -- */
  519. nlone = 0;
  520. for(i=0;i<65000;i++) {
  521.     if (1 == lonevdata[i]) {
  522.          if (nlone < asize) { /* insert into idarray up till asize */
  523.             idarray[nlone] = i;
  524.             }
  525.         nlone ++;
  526.         }
  527.    }
  528. DFIfreespace(lonevdata);
  529.  
  530. return(nlone); /* return the TOTAL # of lone vdatas */
  531.  
  532. } /* VSlone */
  533.  
  534. /* ================================================================== */
  535. /*
  536. * Vlone  - returns an array of refs of all lone vgroups in the file.
  537. *           - returns -1 if error
  538. *          - otherwise returns the total number of lone vgroups in the file 
  539. *
  540. *            If idarray is too small, routine will only fill idarray with up
  541. *             to asize worth of refs.
  542. *
  543. *            INPUT idarray: user supplies  an int array.
  544. *           INPUT asize: integer specifying how many ints in idarray[];
  545. *            INPUT f: HDF file pointer.
  546. *
  547. */
  548.  
  549. PUBLIC int Vlone(f, idarray, asize) 
  550.  
  551. DF * f;
  552. int idarray[];             /* array to contain the refs */
  553. int asize;            /* input: size of idarray */
  554. {
  555. int         * lonevg; /* local working area: stores flags of vgroups */
  556. int        i;
  557. int         vgid, vstag, id;
  558. VGROUP     * vg;
  559. int         dum;
  560. int         nlone; /* total number of lone vgroups */
  561.  
  562.  
  563. /* -- allocate space for vgroup refs, init to zeroes -- */
  564. if (NULL == (lonevg = (int*) DFIgetspace( 65000 * sizeof(int)))) 
  565.    RTNEG ("Vlone: space too low. Vlone not executed\n");
  566. for(i=0;i<65000;i++) lonevg[i] = 0;
  567.  
  568. /* -- look for all vgroups in the file, and flag (1) each -- */
  569. id = -1;
  570. dum=0;
  571. while(1) {
  572.     if ( -1 == (id = Vgetid (f, id))) break; /* no more vgroups */
  573.     lonevg[ id ] = 1;
  574.     }
  575.  
  576. /* -- Look through all vgs, searching for vgroups -- */
  577. /* -- increment its index in lonevg if found -- */
  578. dum=0;
  579. vgid = -1;
  580. while(1) {
  581.     if ( -1 == (vgid = Vgetid (f, vgid))) break; /* no more vgroups */
  582.    printf("Vlone: vgid=%d..attach",vgid);
  583.     vg = (VGROUP*) Vattach(f,vgid,"r");
  584.    printf("..attach done\n");
  585.     id = -1;
  586.     for (i=0; i< Vntagrefs(vg); i++) {
  587.         Vgettagref (vg, i, &vstag, &id);
  588.         if (vstag==VGDESCTAG) { lonevg[id]++; dum++;  }
  589.         }
  590.     Vdetach(vg);
  591.     }
  592.  
  593. /* -- check in lonevg: it's a lone vgroup if its flag is still 1 -- */
  594. nlone = 0;
  595. for(i=0;i<65000;i++) {
  596.     if (1 == lonevg[i]) {
  597.          if (nlone < asize) { /* insert into idarray up till asize */
  598.             idarray[nlone] = i;
  599.             }
  600.         nlone ++;
  601.         }
  602.    }
  603. DFIfreespace(lonevg);
  604.  
  605. return(nlone); /* return the TOTAL # of lone vgroups */
  606.  
  607. } /* Vlone */
  608.  
  609.  
  610. /* ================================================================== */
  611. /* new jan 3 1991 */
  612. /* looks in the file and returns the ref of the vgroup with name vgname */
  613. /* 
  614. * returns -1 if not found, or error.
  615. * otherwise, returns the vgroup's ref (a +ve integer).
  616. */
  617.  
  618. int Vfind (f, vgname)
  619. DF * f;
  620. char * vgname;
  621. {
  622.   int vgid = -1;
  623.   VGROUP* vg;
  624.   char name[512];
  625.  
  626.     while ( -1 != (vgid=Vgetid(f, vgid)) ) {
  627.         vg = (VGROUP*) Vattach(f,vgid,"r");
  628.         if (vg==NULL) return(-1);             /* error */
  629.         Vgetname(vg, name);
  630.         Vdetach (vg);
  631.         if (!strcmp(vgname,name)) return (vg->oref);  /* found the vgroup */
  632.       }
  633.       return(-1); /* not found */
  634.  
  635. } /* Vfind */
  636.  
  637. /* ================================================================== */
  638. /* new jan 3 1991 */
  639. /* looks in the file and returns the ref of the vdata with name vsname */
  640. /* 
  641. * returns -1 if not found, or error.
  642. * otherwise, returns the vdata's ref (a +ve integer).
  643. */
  644.  
  645. int VSfind (f, vsname)
  646. DF * f;
  647. char * vsname;
  648. {
  649.   int vsid = -1;
  650.   VDATA * vs;
  651.   char name[512];
  652.  
  653.     while ( -1 != (vsid=VSgetid(f, vsid)) ) {
  654.         vs = (VDATA*) VSattach(f,vsid,"r");
  655.         if (vs==NULL) return(-1);             /* error */
  656.         VSgetname(vs, name);
  657.         VSdetach (vs);
  658.         if (!strcmp(vsname, name)) return (vs->oref);  /* found the vdata */
  659.       }
  660.       return(-1); /* not found */
  661.  
  662. } /* VSfind */
  663.  
  664. /* ================================================================== */
  665.  
  666. /*
  667. * Vsetzap: Useless now. Maintained for back compatibility.
  668. */
  669.  
  670. Vsetzap() {
  671.     if (vjv) { sprintf(sjs,"Vsetzap: defunct\n"); zj; }
  672. }
  673. /* ================================================================== */
  674.